/*
 * trackerlogger.c
 *
 *  Created on: 24/05/2009
 *      Author: andre
 */
#include <stdio.h>
#include <string.h>
#include <trackerlogger.h>

//global logging level for the tracker
int LOG_LEVEL = DEBUG;

int logCriticalError (char *functionName, char *errorMessage, char *file, int line, int errornumber ){

    if ( LOG_LEVEL <= CRITICAL){
        return logMessage("Critical Error", functionName, errorMessage, file, line, errornumber);
    }

    return 0;
}

int logError (char *functionName, char *errorMessage, char *file, int line, int errornumber ){

    if ( LOG_LEVEL <= ERROR){
        return logMessage("Error", functionName, errorMessage, file, line, errornumber);
    }

    return 0;
}

int logWarning (char *functionName, char *errorMessage, char *file, int line, int errornumber ){

    if ( LOG_LEVEL <= WARN){
        return logMessage("Warning", functionName, errorMessage, file, line, errornumber);
    }

    return 0;
}

int logInfo (char *message){
    if ( LOG_LEVEL <= INFO){
        fprintf (stdout, "Info: %s\n", message);
    }
    return 0;
}

int logVerboseInfo (char *functionName, char *errorMessage, char *file, int line, int errornumber ){


    if ( LOG_LEVEL <= INFO){
        return logMessage("Info", functionName, errorMessage, file, line, errornumber);
    }

    return 0;
}

int logDebugMessage (char *functionName, char *debugMessage, char *file, int line, int errornumber ){

    if ( LOG_LEVEL <= DEBUG){
        return logMessage("Debug Message", functionName, debugMessage, file, line, errornumber);
    }

    return 0;
}

int logStackTrace (char *functionName, char *file, int line ){

    if ( LOG_LEVEL <= ERROR){
        fprintf (stderr, "StackTrace: \t %s:%s na linha %d\n", file, functionName, line-4);
    }

    return 0;
}


int logMessage (char *messageType, char *functionName, char *errorMessage, char *file, int line, int errorNumber ){

    fprintf (stderr, "%s em \t %s:%s na linha %d: %s \n",messageType, file, functionName, line-4, errorMessage);

    if (errorNumber !=0){
        char errnoMsg[100];
        strerror_r(errorNumber, errnoMsg, 100);
        fprintf (stderr,"Errno code:%d Error Message: %s \n", errorNumber, errnoMsg);
    }

    return 0;
}
